package com.book.book.controller;

import com.book.book.constant.Constants;
import com.book.book.enums.ResultCodeEnum;
import com.book.book.model.*;
import com.book.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {

    @Autowired
    private BookService bookService;
    @RequestMapping("/getList")
    public List<BookInfo>getList(){
//        BookService bookService=new BookService();
        return bookService.getList();
    }

    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo){
        log.info("添加图书，Request："+bookInfo);
        //参数校验
        if(!StringUtils.hasLength(bookInfo.getBookName())
        ||!StringUtils.hasLength(bookInfo.getAuthor())
        ||bookInfo.getCount()==null
                ||bookInfo.getPrice()==null
                ||!StringUtils.hasLength(bookInfo.getPublish())
                ||bookInfo.getStatus()==null
        ){
            log.warn("添加图书，参数不合法，request：{}",bookInfo);
            return "参数不合法";
        }
        try{
            bookService.addBook(bookInfo);
            return "";
        }catch (Exception e){
            log.error("添加图书异常,e:",e);
            return "添加图书异常";
        }
    }


    @RequestMapping("/getListByPage")
    public Result<ResponseResult<BookInfo>>getListByPage(PageRequest pageRequest, HttpSession session){
//        if (session.getAttribute(Constants.SESSION_USER_KEY)==null){
//            return Result.unlogin();
//        }
//        UserInfo userInfo=(UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
//        if (userInfo==null||userInfo.getId()<=0){
//            Result.unlogin();
//        }
ResponseResult<BookInfo>listByPage=bookService.getListByPage(pageRequest);
        return Result.succes(listByPage);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
log.info("查询图书信息,bookId="+bookId);
return bookService.queryBookById(bookId);
    }

    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo){
        log.info("修改图书，bookInfo :{}",bookInfo);
        try{
            bookService.updateBook(bookInfo);
            log.info("修改图书成功");
            return "";
        }catch (Exception e){
            log.info("修改图书发生异常");
            return e.getMessage();
        }
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(Integer bookId){
        log.info("删除图书, bookId:{}",bookId);
        try{
           BookInfo bookInfo= bookService.queryBookById(bookId);
           bookInfo.setStatus(0);
            updateBook(bookInfo);
            log.info("删除图书成功");
            return "";
        }catch (Exception e){
           return e.getMessage();
        }
    }

    @RequestMapping("/batchDeleteBook")
public Boolean batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书,ids:{}",ids);
        try{
            bookService.batchDeleteBook(ids);
            log.info("批量删除图书成功");
            return true;
        }catch (Exception e){
         log.error("批量删除图书失败 e:",e);
         return false;
        }
    }


}
